﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using WebSiteAdminTool.UI.MembershipService;
using WebSiteAdminTool.UI.Utils;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using WebSiteAdminTool.UI.Utils.Commands;
using GalaSoft.MvvmLight.Messaging;
using WebSiteAdminTool.UI.ViewModels.Base;
using WebSiteAdminTool.UI.Services.MessageBoxes;
using WebSiteAdminTool.UI.Ioc;

namespace WebSiteAdminTool.UI.ViewModels.Security.Users
{
    public class CreateUserViewModel : BaseDialogViewModel
    {
        #region private data

        private UserData _user = null;
        private UserPasswordConstraintsData _passwordConstraints = null;
        private string _password = null;
        private string _confirmedPassword = null;
        // commands
        private GenericCommand _generatePasswordCommand;

        #endregion

        #region properties

        // commands
        public GenericCommand GeneratePasswordCommand { get { return _generatePasswordCommand; } }

        public UserData User
        {
            get { return _user; }
            set { _user = value; }
        }

        public Visibility PasswordQuestionVisibility
        {
            get
            {
                if (null != PasswordConstraints)
                    return PasswordConstraints.RequiresQuestionAndAnswer ? Visibility.Visible : Visibility.Collapsed;
                return Visibility.Collapsed;
            }
        }

        private UserPasswordConstraintsData PasswordConstraints
        {
            get { return _passwordConstraints; }
            set
            {
                _passwordConstraints = value;
                if (null != value)
                {
                    RaisePropertyChanged("PasswordQuestionVisibility");
                }
            }
        }

        public string Password
        {
            get { return _password; }
            set
            {
                _password = value;
                RaisePropertyChanged("Password");
            }
        }

        public string ConfirmedPassword
        {
            get { return _confirmedPassword; }
            set
            {
                _confirmedPassword = value;
                RaisePropertyChanged("ConfirmedPassword");
            }
        }

        public override string DialogTitle
        {
            get { return "Create User"; }
        }

        #endregion

        #region data loading methods

        protected override void LoadData()
        {
            base.LoadData();

            var client = new MembershipServiceClient();
            client.GetPasswordConstraintsCompleted += new EventHandler<GetPasswordConstraintsCompletedEventArgs>(client_GetPasswordConstraintsCompleted);
            client.GetPasswordConstraintsAsync();
        }

        void client_GetPasswordConstraintsCompleted(object sender, GetPasswordConstraintsCompletedEventArgs e)
        {
            if (e.IsServiceCallSuccessfull() && null != e.Result)
            {
                PasswordConstraints = e.Result;
            }
        }

        protected override void DoSave()
        {
            if (null != this.User)
            {
                var client = new MembershipServiceClient();
                CreateUser(client);
            }
        }

        private void CreateUser(MembershipServiceClient client)
        {
            client.CreateUserCompleted += new EventHandler<CreateUserCompletedEventArgs>(client_CreateUserCompleted);
            client.CreateUserAsync(this.User, this.Password, null);
        }

        void client_CreateUserCompleted(object sender, CreateUserCompletedEventArgs e)
        {
            SaveCompleted<UserData>(e, e.Result);
        }

        private void GeneratePassword(object data)
        {
            var client = new MembershipServiceClient();
            client.GenerateValidPasswordCompleted += new EventHandler<GenerateValidPasswordCompletedEventArgs>(client_GenerateValidPasswordCompleted);
            client.GenerateValidPasswordAsync();
        }

        private void client_GenerateValidPasswordCompleted(object sender, GenerateValidPasswordCompletedEventArgs e)
        {
            if (e.IsServiceCallSuccessfull() && !string.IsNullOrWhiteSpace(e.Result))
            {
                Factory.ServiceLocator.MessageBoxService.Show(string.Format("The generated password is: {0}\nPlease provide it to user after the changes are saved.", e.Result), "New Password");
                Password = ConfirmedPassword = e.Result;
            }
        }

        #endregion

        #region overriden base methods

        protected override void InitializeCommands()
        {
            _generatePasswordCommand = new GenericCommand(GeneratePassword, true);
        }

        #endregion
    }
}
